#pragma GCC optimize(2)
#include <bits/stdc++.h>

using namespace std;
#define int long long
const int inf = 0x3f3f3f3f3f3f3f3f;

void solve()
{
	int a, b;
	cin >> a >> b;
	int ans = -1;
	if ((a + b) % 2 == 0) ans = a + b;
	if (a % 2 == 0 && b % 2 == 1)
	{
		cout << -1 << '\n';
		return;
	}
	else if (a % 2 == 1 && b % 2 == 1) ans = max(ans, a * b + 1);
	else if (a % 2 == 0 && a % 2 == 0) ans = max(ans, a * (b / 2) + 2);
	else
	{
		if (b % 2 == 0 && b % 4 == 1)
		{
			cout << -1 << '\n';
			return;
		}
		else if (b % 4 == 0) ans = max(ans, a * (b / 2) + 2);
	}
	cout << ans << '\n';
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int _ = 1;
	cin >> _;
	while (_--)
	{
		solve();
	}
	return 0;
}